package com.guangzhi.weijianzhi.activity;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.CycleInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
import cn.sharesdk.framework.ShareSDK;
import com.guangzhi.weijianzhi.R;
import com.guangzhi.weijianzhi.classify.MainClassifyFragment;
import com.guangzhi.weijianzhi.company.center.ComCenterFragment;
import com.guangzhi.weijianzhi.company.manager.ComapnyMangFragment;
import com.guangzhi.weijianzhi.context.WeiJianZhiEntity;
import com.guangzhi.weijianzhi.maincenter.UserCenterFragment;
import com.guangzhi.weijianzhi.mainhome.MainHomeFragment;
import com.guangzhi.weijianzhi.mainsmsg.MainUserMsgFragment;
import com.guangzhi.weijianzhi.view.CustomViewPager;
import com.umeng.update.UmengUpdateAgent;
/**
 * 主页
 * @author Administrator
 *
 */
public class MainPageActivity extends BaseActivity implements OnCheckedChangeListener {
	private long mkeyTime = 0;
	private CustomViewPager mViewPager;
	private RadioGroup mGroup;
	private Handler mHandler = new Handler();
	private boolean isUser;
	private RadioButton mRadio_0ne;
	private RadioButton mRadio_two;
	private RadioButton mRadio_three;
	private RadioButton mRadio_four;
	private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
	private ArrayList<Fragment> fragmentsUser = new ArrayList<Fragment>();
	private  MainHomeFragment CompanyHomeFragment =  new MainHomeFragment();
	private  ComapnyMangFragment ManagerFragment =  new ComapnyMangFragment();
	private  ComCenterFragment ComCenterFragment =  new ComCenterFragment();
	
	private  MainHomeFragment HomeFragment =  new MainHomeFragment();
	private  MainClassifyFragment ClassifyFragment =  new MainClassifyFragment();
	private  MainUserMsgFragment UserMsgFragment =  new MainUserMsgFragment();
	private  UserCenterFragment ConterFragment =  new UserCenterFragment();
	
	private MyAdapter mAdapter;
	private boolean isResume;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tz_mainpage_layout);
		 ShareSDK.initSDK(this);//分享SDK
		 isResume = getIntent().getBooleanExtra("isResume",false);
		 addFragment();
		 initView();
	}
	
	private void initView() {
		initUpdateAgent();
	    mRadio_0ne = (RadioButton) findViewById(R.id.radio_one);
	    mRadio_two = (RadioButton) findViewById(R.id.radio_two);
	    mRadio_three = (RadioButton) findViewById(R.id.radio_three);
	    mRadio_four = (RadioButton) findViewById(R.id.radio_four);
		mViewPager = (CustomViewPager) findViewById(R.id.vPager);
		mViewPager.setScrollble(false);
		mGroup = (RadioGroup) findViewById(R.id.main_group);
		mAdapter = new MyAdapter(getSupportFragmentManager());
		mViewPager.setAdapter(mAdapter);
		mGroup.setOnCheckedChangeListener(this);

		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		isUser = WeiJianZhiEntity.isUser();
		if(1==WeiJianZhiEntity.isLogin || isResume){//只有是从登陆界面过来或者是第一次进来才会区分
			if(isUser){
				mRadio_0ne.setText("首页");
				mRadio_two.setText("分类");
				mRadio_three.setText("工作");
				mRadio_four.setText("个人中心");
				mRadio_four.setVisibility(View.VISIBLE);
				mAdapter.onBundleData(fragmentsUser);
			}else{
				mRadio_0ne.setText("首页");
				mRadio_two.setText("管理");
				mRadio_three.setText("个人中心");
				mRadio_four.setVisibility(View.GONE);
				mAdapter.onBundleData(fragments);
			}
			mGroup.check(R.id.radio_one);
			mViewPager.setCurrentItem(0);
		}

	}

	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		WeiJianZhiEntity.isLogin =0;
		isResume =false;
	}
	
	
	private class MyAdapter extends FragmentStatePagerAdapter{
		private ArrayList<Fragment> lists = new ArrayList<Fragment>();
		public MyAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return lists.get(arg0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return lists.size();
		}
		public void onBundleData(ArrayList<Fragment>list){
		this.lists.clear();
		this.lists.addAll(list);
		this.notifyDataSetChanged();
	}
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    //二次返回退出
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	        if ((System.currentTimeMillis() - mkeyTime) > 2000) {
	            mkeyTime = System.currentTimeMillis();
	            Toast.makeText(this, "再按一次就退出了。。。", Toast.LENGTH_LONG).show();
	        } else {
	        	//关闭activity退出程序，并杀死进程
	            finish();
	            System.exit(0);
	            android.os.Process.killProcess(android.os.Process.myPid());
	        }
	        return false;
	    }
	    return super.onKeyDown(keyCode, event);
	}
  
	private void addFragment() {
		// TODO Auto-generated method stub
		fragments.clear();
		fragments.add(CompanyHomeFragment);
		fragments.add(ManagerFragment);
		fragments.add(ComCenterFragment);
		
		fragmentsUser.clear();
		fragmentsUser.add(HomeFragment);
		fragmentsUser.add(ClassifyFragment);
		fragmentsUser.add(UserMsgFragment);
		fragmentsUser.add(ConterFragment);
	}
	

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		
		RadioButton rb = (RadioButton) findViewById(checkedId);
		if (rb!=null) {
			startRt(rb);
			
		}
		switch (checkedId) {
		case R.id.radio_one:
			if(mViewPager!=null){
				mViewPager.setCurrentItem(0,true);
			}
			break;
		case R.id.radio_two:
			if(mViewPager!=null){
				mViewPager.setCurrentItem(1,true);
			}
			break;
		case R.id.radio_three:
			if(mViewPager!=null){
				mViewPager.setCurrentItem(2,true);
			}
			break;
		case R.id.radio_four:
			if(mViewPager!=null){
				mViewPager.setCurrentItem(3,true);
			}
			break;

		}
		
	}
	private void startRt(View v) {
		int time = 500;
		float degree = 2.0f;
		AnimationSet set = new AnimationSet(true);
		set.setInterpolator(new CycleInterpolator(3));

		RotateAnimation ro1 = new RotateAnimation(0f, degree,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0f);
		RotateAnimation ro4 = new RotateAnimation(-degree, 0f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0f);
		
		set.addAnimation(ro1);
		set.addAnimation(ro4);
	    set.setDuration(time);
	   
	    v.startAnimation(set);
	}
	//更新
	private void initUpdateAgent() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				UmengUpdateAgent.update(MainPageActivity.this);
				UmengUpdateAgent.setUpdateOnlyWifi(false);
			}
		}, 500);
	}
}
